//
//  TKIMYcMsgView.m
//  TKIMSDK
//
//  Created by TryHone on 16/6/30.
//  Copyright © 2016年 TryHone. All rights reserved.
//

#import "TKIMYcSMSMsgView.h"

@interface TKIMYcSMSMsgView()

//图文消息标题
@property(nonatomic,strong) UILabel *titleLabel;
//图文消息时间
@property(nonatomic,strong) UILabel *timeLabel;
//中间分隔线
@property(nonatomic,strong) UILabel *splitLine;//中间的分隔线
//内容摘要描述
@property(nonatomic,strong) UILabel *contentDescription;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic,strong)UIImageView* seleteImage;

@property (nonatomic, strong) UILabel *seeDetailLabel;

@property (nonatomic, strong) UIImageView *arrowImageView;

@end


@implementation TKIMYcSMSMsgView

-(void)drawView
{
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(TKIMScreenWidth*0.025);
        make.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(0.95*TKIMScreenWidth);
    }];
    
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.splitLine];
    [self.bgView addSubview:self.contentDescription];
    [self.bgView addSubview:self.seeDetailLabel];
    [self.bgView addSubview:self.arrowImageView];
    
    
    
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.bgView addGestureRecognizer:longPress];
}

-(UIImageView*)seleteImage{
    
    if (_seleteImage==nil) {
        _seleteImage=[[UIImageView alloc] init];
        _seleteImage.image=TKIMGetImage(@"icon_uncheck");
    }
    return _seleteImage;
}

//切换选中样式
-(void)choseSeletedImageState:(BOOL)flag{
    
    if (flag) {
        self.seleteImage.image=TKIMGetImage(@"icon_checked");
    }else{
        self.seleteImage.image=TKIMGetImage(@"icon_uncheck");
    }
}

-(void)setIsShowSeleted:(BOOL)isShowSeleted{
    
    if (isShowSeleted) {
        self.seleteImage.frame=CGRectMake(10, 10, 22, 22);
        [self addSubview:self.seleteImage];
        
        
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(52);
        }];
        
    }else{
        [self.seleteImage removeFromSuperview];
        self.seleteImage=nil;
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(TKIMScreenWidth*0.025);
        }];
    }

}


-(UILabel*)titleLabel{
  
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake(10, 10, TKIMScreenWidth*0.95-20, 15);
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        _titleLabel.textColor =[TKUIHelper colorWithHexString:@"#333333"];//[UIColor blackColor];

    }
    return _titleLabel;

}


-(UILabel *)timeLabel
{
    if (!_timeLabel)
    {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.font = [UIFont systemFontOfSize:13.0f];
        _timeLabel.textColor =[TKUIHelper colorWithHexString:@"#666666"];// [UIColor grayColor];
        _timeLabel.textAlignment=NSTextAlignmentRight;
    }
    return _timeLabel;
}

-(UILabel *)contentDescription
{
    if (!_contentDescription)
    {
        _contentDescription = [[UILabel alloc] init];
        _contentDescription.frame = CGRectMake(self.titleLabel.left, self.splitLine.bottom+10, self.titleLabel.width, 35);
        _contentDescription.backgroundColor = [UIColor clearColor];
        _contentDescription.font = [UIFont systemFontOfSize:14.0f];
        _contentDescription.textColor = [TKUIHelper colorWithHexString:@"#333333"];
        _contentDescription.numberOfLines = 0;
        _contentDescription.lineBreakMode = NSLineBreakByCharWrapping;
        
    }
    return _contentDescription;
}


-(UILabel *)splitLine
{
    if (!_splitLine)
    {
        _splitLine = [[UILabel alloc] init];
        _splitLine.frame = CGRectMake(self.titleLabel.left, self.titleLabel.bottom+10, self.titleLabel.width, 1);
        _splitLine.backgroundColor =[TKIMColorHelper stringToColor:@"#f4f4f4"];
    }
    return _splitLine;
}

- (UILabel *)seeDetailLabel
{
    if (!_seeDetailLabel) {
        _seeDetailLabel = [[UILabel alloc]init];
        _seeDetailLabel.frame = CGRectMake(self.titleLabel.left, self.contentDescription.bottom+10, self.titleLabel.width, 20);
        _seeDetailLabel.font = [UIFont systemFontOfSize:14];
        _seeDetailLabel.textColor = [TKUIHelper colorWithHexString:@"#333333"];
        _seeDetailLabel.text = @"查看详情";
    }
    return _seeDetailLabel;
}

- (UIImageView *)arrowImageView
{
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc]init];
        _arrowImageView.frame = CGRectMake(self.titleLabel.right-20, self.seeDetailLabel.top, 20, 20);
        _arrowImageView.image = [UIImage imageNamed:TKIMPhotoName(@"im_icon_rigthArrow")];
    }
    return _arrowImageView;
}

-(void)setMessage:(TKIMMessage *)message{
   [super setMessage:message];
    TKIMSrvRichMessage* msg ;//= [[TKIMSrvRichMessage alloc] init];
     msg =(TKIMSrvRichMessage *)message;
    if (msg&&msg.items.count>0) {
        TKIMSrvRichItem*item=(TKIMSrvRichItem*)msg.items[0];
        self.titleLabel.text=item.title;
        self.contentDescription.text=item.contentDescription;
        self.timeLabel.text=[TKIMDateHelper formattedTime:msg.time];
    }
    
    if (msg.state==TKIMMsgSendStateRead) {
        _titleLabel.textColor =[TKUIHelper colorWithHexString:@"#9A9A9A"];//
        _timeLabel.textColor =[TKUIHelper colorWithHexString:@"#9A9A9A"];//
        _contentDescription.textColor = [TKUIHelper colorWithHexString:@"#9A9A9A"];
        self.seeDetailLabel.textColor = [TKUIHelper colorWithHexString:@"#9A9A9A"];
        
    }else{
       _titleLabel.textColor =[TKUIHelper colorWithHexString:@"#333333"];//
       _timeLabel.textColor =[TKUIHelper colorWithHexString:@"#333333"];//
       _contentDescription.textColor = [TKUIHelper colorWithHexString:@"#666666"];
        self.seeDetailLabel.textColor = [TKUIHelper colorWithHexString:@"#333333"];
    }

    CGSize size = [self.contentDescription sizeThatFits:CGSizeMake(self.contentDescription.width, CGFLOAT_MAX)];
    self.contentDescription.frame = CGRectMake(self.contentDescription.left, self.contentDescription.top, TKIMScreenWidth*0.95-20, size.height+15);

    self.bgView.frame = CGRectMake(TKIMScreenWidth*0.025, 0, 0.95*TKIMScreenWidth, CGRectGetMaxY(self.contentDescription.frame)+3+20);
    self.bgView.backgroundColor = [UIColor whiteColor];
    
    
    self.frame = CGRectMake(self.left, self.top, self.bgView.width, self.bgView.height);
    message.size = self.frame.size;
    
    self.seeDetailLabel.frame = CGRectMake(self.titleLabel.left, self.bgView.height-20-3, self.titleLabel.width, 20);
    self.arrowImageView.frame = CGRectMake(self.titleLabel.right-20, self.seeDetailLabel.top, 20, 20);
}

/**
 *  @Author lpm, 15-11-05 09:11:23
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    TKIMSrvRichMessage *m = (TKIMSrvRichMessage *)msg;
    TKIMSrvRichItem *item = (TKIMSrvRichItem *)m.items.lastObject;
    CGSize size = [TKIMStringHelper sizeForFont:[UIFont systemFontOfSize:14.0] size:CGSizeMake(TKIMScreenWidth*0.8, CGFLOAT_MAX) mode:NSLineBreakByCharWrapping text:item.contentDescription];
    return 91-35+size.height+TKIMChatMsgPadding;
    
//    return 91;
}




@end
